//
//  NYLoginViewModel.m
//  Nooryes
//
//  Created by baken on 2020/2/26.
//  Copyright © 2020 MyApp. All rights reserved.
//

#import "NYLoginViewModel.h"
#import "NYLoginManager.h"
#import "NYSMSVerificationModel.h"
#import "NYLoginModel.h"
#import "NYAccountManager.h"
#import "NYAccountModel.h"
#import <ShareSDK/ShareSDK.h>
#import "NYBindListModel.h"

@implementation NYLoginViewModel

//type 请求类型 1注册 2登录 3找回（修改）密码 4重置手机号
- (void)getVerifyCodeWithCompletionHandle:(CompletionHandle)complete{
    [NYLoginManager getVerifyCodeWithPhone:self.phoneNum andType:self.verifyType CompletionHandle:^(NYSMSVerificationModel *model, NSError *error) {
        if (!error) {
            self.code = model.data;
        }
        
        complete(error);
    }];
}


- (void)thrLoginWithCompletionHandle:(CompletionHandle)complete{
    NSInteger thirdType;
    if ([self.loginPlatform isEqualToString:@"1"]) {
        thirdType = SSDKPlatformTypeQQ;
    } else {
        thirdType = SSDKPlatformTypeWechat;
    }
    
    [ShareSDK authorize:thirdType
               settings:nil
         onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
        if (state == SSDKResponseStateSuccess) {
            NSString *city = [NSString stringWithFormat:@"%@%@",user.rawData[@"province"],user.rawData[@"city"]];
            NSString *unionid =user.rawData[@"unionid"];
            NSInteger myGender = 0;
            switch (user.gender) {
                case 0:
                    myGender = 1;
                    break;
                case 1:
                    myGender = 2;
                    break;
                default:
                    break;
            }
            
            [NYLoginManager thrLoginWithOpenID:unionid andType:self.loginPlatform andName:user.nickname andImg:user.icon andSexe:STR_FROM_INT(myGender) andCity:city  CompletionHandle:^(LoginModel *model, NSError *error) {
                if (!error) {
                    [[NSUserDefaults standardUserDefaults] setObject:model.userid forKey:@"userid"];
                    [[NSUserDefaults standardUserDefaults] setObject:model.sessionid forKey:@"sessionid"];
                    [NYAccountManager getUserInfoWithUserID:model.userid andSessionID:model.sessionid andCompletionHandle:^(NYAccountModel * model, NSError * error) {
                        if (!error) {
                            myDelegate.account = model.data;
                            [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:self];
                        }
                        complete(error);
                    }];
                } else {
                    complete(error);
                }
            }];
        }
        else if (state == SSDKResponseStateCancel) {
            complete(error = [NYLoginManager createError:@"取消登录"]);
        }
        else if (state == SSDKResponseStateFail){
            complete(error);
        }
    }];
}

- (void)logoutWithCompletionHandle:(CompletionHandle)complete{
    //    [NYLoginManager logoutWithUserid:[[NSUserDefaults standardUserDefaults]objectForKey:@"userid"] andSessionid:[[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"] andCompletionHandle:^(LoginModel *account, NSError *error){
    //        if (!error) {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userid"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"sessionid"];
    myDelegate.account = nil;
    
    //        }
    complete(nil);
    //    }];
}

- (void)phonePasswordLoginWithCompletionHandle:(CompletionHandle)complete{
    __block NSString *uid;
    __block NSString *sessionid;
    //    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    [NYLoginManager loginWithPhoneNum:self.phoneNum andPassword:self.password andCompletionHandle:^(LoginModel *account, NSError *error) {
        if (!error) {
            uid = account.userid;
            sessionid = account.sessionid;
            [[NSUserDefaults standardUserDefaults] setObject:account.userid forKey:@"userid"];
            [[NSUserDefaults standardUserDefaults] setObject:account.sessionid forKey:@"sessionid"];
            [NYAccountManager getUserInfoWithUserID:uid andSessionID:sessionid andCompletionHandle:^(NYAccountModel * model, NSError * error) {
                if (!error) {
                    myDelegate.account = model.data;
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:self];
                }
                complete(error);
            }];
            
        }
        complete(error);
        //        dispatch_semaphore_signal(semaphore);
    }];
    
    //    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    
}

- (void)phoneCodeLoginWithCompletionHandle:(CompletionHandle)complete{
    __block NSString *uid;
    __block NSString *sessionid;
    //    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    [NYLoginManager registerAndLoginWithPhoneNum:self.phoneNum andVerifyCode:self.code andCompletionHandle:^(LoginModel *account, NSError *error) {
        if (!error) {
            uid = account.userid;
            sessionid = account.sessionid;
            [[NSUserDefaults standardUserDefaults] setObject:account.userid forKey:@"userid"];
            [[NSUserDefaults standardUserDefaults] setObject:account.sessionid forKey:@"sessionid"];
            [NYAccountManager getUserInfoWithUserID:uid andSessionID:sessionid andCompletionHandle:^(NYAccountModel * model, NSError * error) {
                if (!error) {
                    myDelegate.account = model.data;
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:self];
                }
                complete(error);
            }];
        }
        complete(error);
        //        dispatch_semaphore_signal(semaphore);
    }];
    //    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    
}

- (void)getThr3BindListWithCompletionHandle:(CompletionHandle)complete{
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    [NYLoginManager getThr3BindListWithUserID:uid andSessionID:sessionid andCompletionHandle:^(NYBindListModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error) {
            [self.bindArray addObjectsFromArray:model.data];
        }
        complete(error);
    }];
}

- (void)bindThr3WithCompletionHandle:(CompletionHandle)complete{
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    
    NSInteger thirdType;
    if ([self.loginPlatform isEqualToString:@"1"]) {
        thirdType = SSDKPlatformTypeQQ;
    } else {
        thirdType = SSDKPlatformTypeWechat;
    }
    [ShareSDK authorize:thirdType
               settings:nil
         onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
        if (state == SSDKResponseStateSuccess) {
            NSString *city = [NSString stringWithFormat:@"%@%@",user.rawData[@"province"],user.rawData[@"city"]];
            NSString *unionid =user.rawData[@"unionid"];
            NSInteger myGender = 0;
            switch (user.gender) {
                case 0:
                    myGender = 1;
                    break;
                case 1:
                    myGender = 2;
                    break;
                default:
                    break;
            }
            [NYLoginManager bindThr3WithUserID:uid andSessionID:sessionid andUnionid:unionid andType:self.loginPlatform andHead:user.icon andNickname:user.nickname andSex:STR_FROM_INT(myGender) andCity:city andCompletionHandle:^(NYMainModel * _Nonnull model, NSError * _Nonnull error) {
                if (!error) {
                    [NYAccountManager getUserInfoWithUserID:uid andSessionID:sessionid andCompletionHandle:^(NYAccountModel * model, NSError * error) {
                        if (!error) {
                            myDelegate.account = model.data;
                            [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:self];
                        }
                        complete(error);
                    }];
                } else {
                    complete(error);
                }
            }];
        }
        else if (state == SSDKResponseStateCancel) {
            complete([NYLoginManager createError:@"取消绑定"]);
        }
        else if (state == SSDKResponseStateFail){
            complete(error);
        }
    }];
    
}


-(NSMutableArray *)bindArray{
    if (!_bindArray) {
        _bindArray = [NSMutableArray array];
    }
    return _bindArray;
}
@end
